#ifndef _ASSETS_H_
#define _ASSETS_H_

#include <map>
#include <string>

template <class Ass>
class Assets
{
public:
	typedef typename std::map<std::string, Ass>::iterator Iter;

	Ass* Get(const char *name)
	{
		return Get(std::string(name));
	}

	Ass* Get(const std::string & name)
	{
		Iter it = assets.find(name);
		if (it == assets.end())
		{
			std::pair<std::string, Ass> entry;
			entry.first += name;
			entry.second.Open(name);
			it = assets.insert(entry).first;
		}
		return &it->second;
	}

	int BatchGet(Ass *batch[], int n, const char *dir, const char *names[])
	{
		int got = 0;

		for (int i = 0; i < n; ++i)
		{
			batch[i] = NULL;

			if (names[i])
			{
				std::string name;
				if (dir)
					name += dir;
				name += names[i];

				batch[i] = Get(name);
				if (batch[i])
					++got;
			}
		}

		return got;
	}

	void Close(const char *name)
	{
		if (name)
			Close(std::string(name));
	}

	void Close(const std::string & name)
	{
		Iter it = assets.find(name);
		if (it != assets.end())
		{
			it->second.Close();
			assets.erase(it);
		}
	}

	void Clear()
	{
		for (Iter it = assets.begin();
			it != assets.end();
			++it)
		{
			it->second.Close();
		}

		assets.clear();
	}

private:
	std::map<std::string, Ass> assets;
};

#endif
